package com.CGFrame.gantt;

import java.awt.Color;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;

import javax.swing.JApplet;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;

import com.CGFrame.struts.POJOs.Element;
import com.CGFrame.struts.POJOs.Elementgroup;
import com.CGFrame.struts.POJOs.Make;
import com.CGFrame.struts.POJOs.Shot;
import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;
import com.CGFrame.struts.bean.keypath.KeypathBean;
import com.CGFrame.struts.bean.keypath.EventNode;

public class MakeGantt extends JApplet implements Gantt {

	String type;

	int id;

	StringBuffer ids = new StringBuffer(); // ���category ���� �� ID

	public void init() {
		type = getParameter("type");
		String str_id = getParameter("id");
		id = Integer.parseInt(str_id);
		// type = "Shot";
		// id = 2;
		drawChart(null, null);

	}

	public void drawChart(String selectType, String selectId) {

		this.ids = new StringBuffer();

		IntervalCategoryDataset dataset = createDateSet(selectType, selectId);
		JFreeChart chart = createChart(dataset);
		CategoryPlot plot = (CategoryPlot) chart.getPlot();
		// plot.getDomainAxis().setMaxCategoryLabelWidthRatio(10.0f);
		plot.setNoDataMessage("��ǰû��Ԫ��");
		// plot.setOrientation(PlotOrientation.HORIZONTAL);
		// plot.addChangeListener();
		CategoryItemRenderer renderer = plot.getRenderer();
		renderer.setSeriesPaint(0, Color.blue);
		renderer.setSeriesPaint(1, Color.darkGray);
		renderer.setItemURLGenerator(new CustomCategoryURLGenerator(ids));
		// renderer.setToolTipGenerator(new CustomGanttToolTips(ids) );
		// add the chart to a panel...

		final ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setAutoscrolls(true);
		// chartPanel.setPreferredSize(new java.awt.Dimension(500, 20*number));
		chartPanel.addChartMouseListener(new MouseListener(this));
		/*
		 * JButton showButton = new JButton("OK");
		 * showButton.setActionCommand("showMake");
		 * showButton.addActionListener(this); chartPanel.add(showButton);
		 * 
		 */
		setContentPane(chartPanel);
		validate();
	}

	IntervalCategoryDataset createDateSet(String selectType, String selectId) // �����type
																				// ,id
																				// �Ǵ�MourseListener��ȡ4��
	{
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		TaskSeries s1 = new TaskSeries("�ƻ�ʱ��");
		KeypathBean keypathBean = new KeypathBean();
		Date startTime = new Date();
		Object obj = null;
		
		if (type.equals("Element")) {
			obj = pop.UniqueQuery("Element", "elementId," + id);
			Element element = (Element) obj;
			startTime = element.getPlanStartTime();
		} else if (type.equals("Shot")) {
			obj = pop.UniqueQuery("Shot", "shotId," + id);
			Shot shot = (Shot) obj;
			startTime = shot.getPlanStartTime();
		}

		startTime.setHours(8);
		EventNode[] makes = keypathBean.Init(obj, type);
		int length = makes.length;

		for (int i = 0; i < length; i++) {
			int makeId = makes[i].getId();
			int makeVe = makes[i].getVe();
			Make make = (Make) pop.UniqueQuery("Make", "makeId," + makeId);
			// System.out.println(make.getMakeName());
			// System.out.println(makes[i].getVe());
			// System.out.println(makes[i].getVl());
			String makeName = make.getMakeName();
			String status = make.getStatus().getStatusName();
			int lastTime = make.getExceptHours();
			Date makeSTime = this.dateAddHour(startTime, makeVe);
			Date makeETime = this.dateAddHour(startTime, makeVe + lastTime);
			// System.out.println(makeSTime.toString()+"-->"+makeETime.toString());
			Task mTask = null;
			try {
				mTask = new Task("����:" + makeName, makeSTime, makeETime);
			} catch (Exception e) {
				System.out.println(make.getMakeName() + "..����.." + e);
				continue;
			}
			if (status.equals("�����")) {
				mTask.setPercentComplete(1);
			} else if (status.equals("��¼��")) {
				mTask.setPercentComplete(0);
			} else if (status.equals("�ȴ�ȷ��")) {
				mTask.setPercentComplete(0.5);
			}
			// System.out.println(make.getMakeId()+make.getMakeName()+"addend");

			s1.add(mTask);
			ids.append("make," + makeId + ";");
		}

		TaskSeriesCollection collection = new TaskSeriesCollection();
		collection.add(s1);

		return collection;
	}

	JFreeChart createChart(final IntervalCategoryDataset dataset) {
		final JFreeChart chart = ChartFactory.createGanttChart(null, // chart
																		// title
				"����", // domain axis label
				"����", // range axis label
				dataset, // data
				true, // include legend
				true, // tooltips
				true // urls
				);

		// chart.addSubtitle(new TextTitle("��ɫ:�����", new Font("����",
		// Font.BOLD,12)));
		// chart.addSubtitle(new TextTitle("��ɫ:δ���", new Font("����",
		// Font.BOLD,12)));
		return chart;
	}

	Date dateAddHour(Date date, int totalHour)// �����ĩ
	{
		int day = totalHour / 8;
		int hour = totalHour % 8;
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		int dayOfWeek1 = cal.get(Calendar.DAY_OF_WEEK); // �ܼ�
		// System.out.println("1�����յģ��������ܼ���"+dayOfWeek1);
		if (dayOfWeek1 == 1)
			cal.add(GregorianCalendar.DATE, 1);
		if (dayOfWeek1 == 7)
			cal.add(GregorianCalendar.DATE, 2);
		for (int i = 1; i <= day; i++) {
			cal.add(GregorianCalendar.DATE, 1);
			int dayOfWeek2 = cal.get(Calendar.DAY_OF_WEEK); // ��1����ܼ�
			// System.out.println("1�����յģ�����һ����������ܼ���"+dayOfWeek2);
			if (dayOfWeek2 == 7)
				cal.add(GregorianCalendar.DATE, 2); // �������ټ�2��
		}
		cal.add(GregorianCalendar.HOUR, hour);
		int dayOfWeek3 = cal.get(Calendar.DAY_OF_WEEK); // ��hour���ܼ�
		if (dayOfWeek3 == 7)
			cal.add(GregorianCalendar.DATE, 2); // �������ټ�2��
		return cal.getTime();
	}

	/*
	 * public void actionPerformed(ActionEvent actionEvent) { 
	 * Auto-generated method stub if
	 * (actionEvent.getActionCommand().equals("showMake")) {
	 * System.out.println("..showMake"); this.showMake = !(this.showMake);
	 * this.setVisible(false); drawChart(); this.setVisible(true);
	 *  } }
	 */
	public StringBuffer getIds() {
		return ids;
	}

}
